package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.TransferService;
import com.ktwlsoft.archivemanage.domain.business.businessService.UnitArchiveManageBusinessService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.ArchiveUnitRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.archiveUnit.UnitArchivesOfYears;
import com.ktwlsoft.archivemanage.payload.basic.*;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.payload.transfer.TransferDetail;
import com.ktwlsoft.archivemanage.payload.unitArchive.UnitArchiveManageAddRequest;
import com.ktwlsoft.archivemanage.payload.unitArchive.UnitArchiveManageCompareRequest;
import com.ktwlsoft.archivemanage.payload.unitArchive.UnitArchiveManageDeleteRequest;
import com.ktwlsoft.archivemanage.payload.unitArchive.UnitArchiveManageUpdateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenkun
 * @version 1.0
 * @description 室藏档案管理控制器类
 * @date 2019-10-28
 */
@Api(value = "室藏档案管理", description = "室藏档案管理")
@RestController
@RequestMapping("/unitArchiveManage")
public class UnitArchiveManageController {

    private final ArchiveUnitRepositoryService archiveUnitRepositoryService;

    private final UnitArchiveManageBusinessService unitArchiveManageBusinessService;

    public UnitArchiveManageController(ArchiveUnitRepositoryService archiveUnitRepositoryService, UnitArchiveManageBusinessService unitArchiveManageBusinessService) {
        this.archiveUnitRepositoryService = archiveUnitRepositoryService;
        this.unitArchiveManageBusinessService = unitArchiveManageBusinessService;
    }

    /**
     * 查询所有立档单位列表
     *
     * @param conditions 查询条件
     * @param enable     账号状态
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "查询立档单位列表")
    @RequestMapping(value = "/findAll", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> findAllArchiveUnitList(@RequestParam int pn, @RequestParam int pageSize,
                                                    @ApiParam(name = "conditions", value = "查询条件") @RequestParam(required = false) String conditions,
                                                    @ApiParam(name = "enable", value = "账号状态") @RequestParam(required = false) Boolean enable) {
        Map<String, Object> requestMap = new HashMap<String, Object>();
        requestMap.put("conditions", conditions);
        requestMap.put("enable", enable);
        Result result = Result.body(archiveUnitRepositoryService.findAllArchiveUnitList(pn, pageSize, requestMap), ResultType.BUSINESS_SUCCESS);
        return ResponseEntity.ok(result);
    }

    /**
     * 分页查询室藏档案列表
     *
     * @param pn       第几页
     * @param pageSize 每页多少条
     * @param unitId   立档单位id
     * @return
     */
    @ApiOperation(value = "分页查询单位室藏档案年度列表")
    @RequestMapping(value = "/findUnitArchives", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> findPagedList(@RequestParam Integer pn,
                                           @RequestParam Integer pageSize,
                                           @ApiParam(name = "unitId", value = "立档单位编号") @RequestParam Long unitId,
                                           @ApiParam(name = "field", value = "排序字段") @RequestParam String field,
                                           @ApiParam(name = "order", value = "排序方式") @RequestParam String order) {
        return ResponseEntity.ok(Result.ok(unitArchiveManageBusinessService.findUnitArchieveByYear(pn, pageSize, unitId, field, order)));
    }

    @ApiOperation("查询单位室藏档案统计详情")
    @GetMapping("/findUnitStatics")
    public ResponseEntity<?> findUnitStatics(@ApiParam("单位id") @RequestParam Long unitId) {
        return ResponseEntity.ok(Result.ok(unitArchiveManageBusinessService.findUnitStatics(unitId)));
    }

    /**
     * 添加室藏档案管理
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "添加室藏档案管理")
    @RequestMapping(value = "/addOne", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> addUnitArchiveManage(@RequestBody UnitArchiveManageAddRequest request,
                                                  BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = unitArchiveManageBusinessService.addUnitArchiveManage(request);
        OperationRepositoryService.saveLog(result, new SaveLogRequest(
                OperationLog.OperationType.ADD,
                "添加" + request.getWriteDate() + "年度室藏档案",
                "室藏档案",
                OperationLog.BusinessType.BUSINESS
        ));
        return ResponseEntity.ok(result);
    }

    /**
     * 删除室藏档案管理
     *
     * @param request
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "删除室藏档案管理")
    @RequestMapping(value = "/deleteOne", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> deleteUnitArchiveManage(@RequestBody UnitArchiveManageDeleteRequest request,
                                                     BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("businessId", request.getBusinessId());
        data.put("archiveId", request.getArchiveIds());
        data.put("unitId", request.getUnitId());
        data.put("year", request.getYear());
        result = unitArchiveManageBusinessService.deleteUnitArchiveManage(data);

        OperationRepositoryService.saveLog(result, new SaveLogRequest(
                OperationLog.OperationType.DELETE,
                "删除" + request.getYear() + "年度室藏档案",
                "室藏档案",
                OperationLog.BusinessType.BUSINESS
        ));

        return ResponseEntity.ok(result);
    }

    /**
     * 编辑室藏档案管理
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity<?>
     */
    @ApiOperation(value = "编辑室藏档案管理")
    @RequestMapping(value = "/updateOne", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> updateUnitArchiveManage(@RequestBody UnitArchiveManageUpdateRequest request,
                                                     BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = unitArchiveManageBusinessService.updateUnitArchiveManage(request);

        OperationRepositoryService.saveLog(result, new SaveLogRequest(
                OperationLog.OperationType.EDIT,
                "编辑" + request.getWriteDate() + "年度室藏档案",
                "室藏档案",
                OperationLog.BusinessType.BUSINESS
        ));

        return ResponseEntity.ok(result);
    }

    /**
     * 同年数据对比
     *
     * @return ResponseEntity
     */
    @ApiOperation(value = "同年数据对比")
    @ResponseBody
    @RequestMapping(value = "/compareSameYearData", produces = "application/json", method = RequestMethod.GET)
    public ResponseEntity<?> compareSameYearData(@RequestBody UnitArchiveManageCompareRequest request,
                                                 BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = unitArchiveManageBusinessService.compareSameYearData(request);
        return ResponseEntity.ok(result);
    }

    /**
     * 同年数据对比确认
     *
     * @param request
     * @param bindingResult
     * @return ResponseEntity
     */
    @ApiOperation(value = "同年数据对比确认")
    @ResponseBody
    @RequestMapping(value = "/compareConfirm", produces = "application/json", method = RequestMethod.POST)
    public ResponseEntity<?> compareConfirm(@RequestBody CompareConfirmRequest request,
                                            BindingResult bindingResult) {
        Result result = null;
        if (bindingResult.hasErrors()) {
            result = Result.body(request, ResultType.CHECK_ERROR);
            result.withMessage(bindingResult.getFieldError().getDefaultMessage());
            return ResponseEntity.ok(result);
        }
        result = unitArchiveManageBusinessService.compareConfirm(request);
        OperationRepositoryService.saveLog(result, new SaveLogRequest(
                OperationLog.OperationType.DATE_COMPARE,
                "对比" + request.getYear() + "年度室藏档案;以“手动添加”为准",
                "室藏档案",
                OperationLog.BusinessType.BUSINESS
        ));
        return ResponseEntity.ok(result);
    }

    /**
     * 注册字符串转成日期的编辑器
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
